package cn.nlsde.controller;

import cn.nlsde.common.ResultInfo;
import cn.nlsde.entity.Category;
import cn.nlsde.entity.Template;
import cn.nlsde.entity.TemplateDetail;
import cn.nlsde.service.CategoryService;
import cn.nlsde.service.TemplateDetailService;
import cn.nlsde.service.TemplateService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by by on 2016/10/21.
 */
@Controller
@RequestMapping("/template")
public class TemplateController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    TemplateService templateService;
    @Autowired
    TemplateDetailService templateDetailService;

    private static final Logger logger = LoggerFactory.getLogger(TemplateController.class);

    /**
     * 查询所有模板
     *
     * @return
     */
    @RequestMapping(value = "/getTemplateAll")
    public ModelAndView getTemplateAll() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("evaluateList");
        int templatecount = templateService.selectCount();
        modelAndView.addObject("templatecount", templatecount);
        return modelAndView;
    }

    /**
     * 查找类别
     *
     * @return
     */
    @RequestMapping(value = "/getCategoryAll")
    public ModelAndView getCategoryAll() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("evaluateOption");
        List<Category> categoryList = categoryService.selectAll();
        modelAndView.addObject("categoryList", categoryList);
        return modelAndView;
    }


    /**
     * 查看已保存的模板
     *
     * @return
     */
    @RequestMapping(value = "/getCategoryAndTemplate")
    public ModelAndView getCategoryAndTemplate(@RequestParam("templateId") Integer templateId) {
        ModelAndView modelAndView = new ModelAndView();
        Template template = templateService.selectById(templateId);
        modelAndView.addObject("template", template);
        List<TemplateDetail> details = templateDetailService.selectByTemplateId(templateId);
        modelAndView.addObject("details", details);
        List<Category> categories = categoryService.selectAll();
        modelAndView.addObject("categoryList", categories);
        modelAndView.setViewName("evaluateOption");
        return modelAndView;
    }

    /**
     * 保存模板
     *
     * @return
     */
    @RequestMapping(value = "saveTemplate")
    public String saveTemplate(HttpServletRequest request,
                               @RequestParam("junzhi") List<Float> means,
                               @RequestParam("fangcha") List<Float> variances,
                               @RequestParam("quanzhong") List<Float> weights,
                               @RequestParam("catid") List<Integer> cats,
                               @RequestParam("detailIds") List<Integer> detailIds,
                               Template template) {
        if (template.getId() == null) {
            templateService.save(template);
        } else {
            templateService.update(template);
        }
        List<TemplateDetail> templateDetails = new ArrayList<>(means.size());
        for (int i = 0; i < means.size(); i++) {
            TemplateDetail t = new TemplateDetail();
            t.setMean(means.get(i));
            t.setVariance(variances.get(i));
            t.setWeight(weights.get(i));
            t.setCategoryId(cats.get(i));
            t.setTemplateId(template.getId());
            //当添加的模板之添加一个指标时detailIds.size()为0，因此需要添加detailIds.size()是否为0的判断
            if (detailIds.size() == 0) {
                templateDetails.add(t);
            } else {
                if (detailIds.get(i) != null && !detailIds.get(i).equals("")) {
                    t.setId(detailIds.get(i));
                    templateDetailService.update(t);
                } else {
                    templateDetails.add(t);
                }
            }
        }
        if (templateDetails.size() > 0)
            templateDetailService.saveBatch(templateDetails);
        return "redirect:getTemplateAll";
    }


    /**
     * 修改模板
     *
     * @return
     */
    public int updateTemplate() {
        Template template = new Template();
        int i = templateService.update(template);
        return i;
    }

    /**
     * 模板列表.
     */
    @RequestMapping(value = "selectTemplateByPage")
    @ResponseBody
    public PageInfo<Template> selectTemplateByPage(PageInfo<Template> templatePageInfo, Integer pageNum) {
        PageInfo<Template> templateList = templateService.selectWithListPage(templatePageInfo);
        return templateList;
    }

    /**
     * 添加category
     */
    @RequestMapping(value = "/addCategory")
    @ResponseBody
    public Map<String, String> addCategory(@RequestParam("category") String category) {
        Map<String, String> map = new HashMap<>();
        Category categorynew = new Category();
        categorynew.setTypeId(5);
        categorynew.setDescription(category);
        map.put("result", "error");
        Integer insertResult = categoryService.save(categorynew);
        if (insertResult != 0) {
            map.put("result", "ok");
            map.put("id", categorynew.getId().toString());
        } else {
            map.put("result", "error");
        }
        return map;
    }

    /**
     * 修改模板删除details
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "deleteDetail")
    @ResponseBody
    public ResultInfo deleteDetails(@RequestParam("ids[]") Integer[] ids) {
        int result = templateDetailService.deleteBathByIds(ids);
        ResultInfo info = new ResultInfo();
        info.setIsSuccess(result != 0);
        return info;
    }

    /**
     * 刪除模板
     */
    @ResponseBody
    @RequestMapping("/deleTemplate")
    public int deleTemplate(Integer templateId) {

        return templateService.deleteById(templateId);
    }

}

